package org.sja.log2traffic.dataStructures.simpleTime;

import org.joda.time.Interval;
import org.joda.time.Period;
import org.joda.time.PeriodType;
import org.sja.log2traffic.ISimpleTimeCalculator;
import org.sja.log2traffic.dataStructures.ConfigurationEntity;
import org.sja.log2traffic.dataStructures.ICategoryTracker;
import org.sja.log2traffic.dataStructures.ILogData;
import org.sja.log2traffic.dataStructures.INewLogInfo;
import org.sja.log2traffic.dataStructures.IOutputerSupport;
import org.sja.log2traffic.dataStructures.ResultColumn;
import org.sja.log2traffic.dataStructures.ResultTable;


public class LogDataSimpleTime implements ILogData, IOutputerSupport{

	private ConfigurationEntity configuration;
	private ISimpleTimeCalculator simpleTimeCalculator;
	private LogDataSimpleTimeElement[] logDataSimpleTimeElements;
	
	public LogDataSimpleTime(ConfigurationEntity configuration, ISimpleTimeCalculator dateFormatter, ICategoryTracker categoryTracker){
		this.configuration = configuration;
		this.simpleTimeCalculator = dateFormatter;
		Period minutes = new Interval(configuration.getFrom(), configuration.getTo()).toPeriod(PeriodType.minutes());
		int howManyElements = minutes.getMinutes();
		this.logDataSimpleTimeElements = new LogDataSimpleTimeElement[howManyElements];
		for (int i=0; i<logDataSimpleTimeElements.length; i++){
			logDataSimpleTimeElements[i] = new LogDataSimpleTimeElement(categoryTracker);
		}
		
	}
	

	@Override
	public INewLogInfo getLogInfo(Long time) {
		if (time < configuration.getFrom()) {
			return null;
		}
		if (time >= configuration.getTo()) {
			return null;
		}
		Period minutes = new Interval(getFrom(),time).toPeriod(PeriodType.minutes());
		return logDataSimpleTimeElements[minutes.getMinutes()];
	}


	@Override
	public ResultTable getTable(ConfigurationEntity configurationFromTo, String category) {
		
		return simpleTimeCalculator.getTableForSimple(this, category);
		
	}
	
	public int getRawDataAmount(){
		return logDataSimpleTimeElements.length;
	}
	
	public LogDataSimpleTimeElement getRawDataElement(int i){
		return logDataSimpleTimeElements[i];
	}
	
	public Long getFrom(){
		return configuration.getFrom();
	}

	public ResultColumn[] getColumnDesciptors(){
		return new ResultColumn[]{new ResultColumn("date"), new ResultColumn("max. request resp. time within 1 minute"), new ResultColumn("amount of requests within 1 minute")};
	}

}